﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Collections;
using KPIS.Utils;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class PS_Absences : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private object _dataItem = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            FillDataToEmployees();
            FillDataToAbsenceTypes();
            FillDataToRecordStatus();

            cmbEmpSeq.SelectedValue = Session["emp_seq"].ToString(); //Convert.ToString(DataBinder.Eval(DataItem, "EMP_SEQ"));
            cmbEmpSeq.Enabled = false;

            if (DataItem != null)
            {
                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    lblSeq.Text = DataBinder.Eval(DataItem, "ABSENCE_SEQ").ToString();
                    //cmbEmpSeq.SelectedValue = Session["emp_seq"].ToString(); //Convert.ToString(DataBinder.Eval(DataItem, "EMP_SEQ"));
                    //cmbEmpSeq.Enabled = false;
                    cmbRecordStatus.SelectedValue = Convert.ToString(DataBinder.Eval(DataItem, "RECORD_STATUS"));
					cmbAbsenceType.SelectedValue = Convert.ToString(DataBinder.Eval(DataItem, "ABSENCE_TYPE_SEQ"));
                    //txtStartDate.Text = DataBinder.Eval(DataItem, "START_DATE", "{0:dd/MM/yyyy}").ToString();
	                txtStartDate2.SetValue(DataBinder.Eval(DataItem, "START_DATE", "{0:dd/MM/yyyy}").ToString());
					rdoStartTime.SelectedValue = DataBinder.Eval(DataItem, "START_TIME").ToString();
					//txtEndDate.Text = DataBinder.Eval(DataItem, "END_DATE", "{0:dd/MM/yyyy}").ToString();
	                txtEndDate2.SetValue(DataBinder.Eval(DataItem, "END_DATE", "{0:dd/MM/yyyy}").ToString());
                    rdoEndTime.SelectedValue = DataBinder.Eval(DataItem, "END_TIME").ToString();
                    txtCountDate.Text = DataBinder.Eval(DataItem, "COUNT_DATE").ToString();
                    txtContactAddress.Text = DataBinder.Eval(DataItem, "CONTACT_ADDRESS").ToString();
                    txtRemarks.Text = DataBinder.Eval(DataItem, "REMARKS").ToString();
                }
                else
                {
                    
                }
                btnUpdate.Visible = !(DataItem is Telerik.Web.UI.GridInsertionObject);
                btnAddnew.Visible = (DataItem is Telerik.Web.UI.GridInsertionObject);
            }
        }

        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }

        protected void FillDataToEmployees()
        {
            //I_HRM_PS_Employees emp = new B_HRM_PS_Employees();

            //DataTable dt = emp.SelectDropDownList("1", "", "");

            //foreach (DataRow dr in dt.Rows)
            //{
            //    RadComboBoxItem item = new RadComboBoxItem();

            //    item.Text = dr["EMP_NAME"].ToString();
            //    item.Value = dr["EMP_SEQ"].ToString();

            //    item.Attributes.Add("EMP_NAME", dr["EMP_NAME"].ToString());
            //    item.Attributes.Add("BIRTH_DATE", dr["BIRTH_DATE"].ToString());
            //    item.Attributes.Add("POS_NAME", dr["POS_NAME"].ToString());
            //    item.Attributes.Add("CLASS_NAME", dr["CLASS_NAME"].ToString());
            //    item.Attributes.Add("JOB_SITE_NAME", dr["JOB_SITE_NAME"].ToString());

            //    cmbEmpSeq.Items.Add(item);

            //    item.DataBind();
            //}

            IAbsenceBLL emp = new AbsenceBLL();
            DataSet ds = emp.GetEmployeeNameList(0);
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                RadComboBoxItem item = new RadComboBoxItem();

                item.Text = dr["FULL_NAME"].ToString();
                item.Value = dr["EMP_SEQ"].ToString();

                cmbEmpSeq.Items.Add(item);

                item.DataBind();
            }
        }

        public void FillDataToAbsenceTypes()
        {
            //I_HRM_PS_AbsenceTypes AbsenceTypes = new B_HRM_PS_AbsenceTypes();
            //IList<AbsenceTypeInfo> infoList = AbsenceTypes.LoadListAbsenceTypes();

            //cmbAbsenceType.DataSource = infoList;

            IAbsenceTypeBLL AbsenceTypes = new AbsenceTypeBLL();
            IList<AbsenceTypeInfo> infoList = AbsenceTypes.ListAbsenceTypes()   ;

            cmbAbsenceType.DataTextField = "Name";
            cmbAbsenceType.DataValueField = "Sequence";
            cmbAbsenceType.DataSource = infoList;

            foreach (AbsenceTypeInfo AbsenceType in infoList)
            {
                RadComboBoxItem item = new RadComboBoxItem();
                item.Text = AbsenceType.Name ;
                item.Value = AbsenceType.Sequence.ToString();
                cmbAbsenceType.Items.Add(item);
                item.DataBind(); 
            }
        }

        public void FillDataToRecordStatus()
        {
            IMasRecordStatusBLL statBLL = new MasRecordStatusBLL();
            IList<MasRecordStatusInfo> infoList = statBLL.ListRecordStatus();

            cmbRecordStatus.DataTextField = "Name";
            cmbRecordStatus.DataValueField = "Status";
            cmbRecordStatus.DataSource = infoList;

            foreach (MasRecordStatusInfo Status in infoList)
            {
                RadComboBoxItem item = new RadComboBoxItem();
                item.Text = Status.Name ;
                item.Value = Status.Sequence.ToString () ;
                cmbAbsenceType.Items.Add(item);
                item.DataBind(); 
            }

        }

        protected void btnAddnew_Click(object sender, EventArgs e)
        {

            //int chkError = Absences.Insert(Int32.Parse(cmbAbsenceType.SelectedValue)
            //    , Int32.Parse(cmbEmpSeq.SelectedValue)
            //    , rdoStartTime.SelectedValue
            //    , rdoEndTime.SelectedValue
            //    , DateTime.Parse(txtStartDate.Text)
            //    , DateTime.Parse(txtEndDate.Text)
            //    , double.Parse(txtCountDate.Text)
            //    , txtContactAddress.Text
            //    , txtRemarks.Text
            //    , cmbRecordStatus.SelectedValue
            //    , 1);
			 
            IAbsenceBLL Absence = new AbsenceBLL();
            Absence.Insert(cmbAbsenceType.SelectedValue
                , cmbEmpSeq.SelectedValue
                , txtStartDate2.GetDateValue()//DateTime.Parse(txtStartDate.Text)
                , rdoStartTime.SelectedValue
				, txtEndDate2.GetDateValue()//DateTime.Parse(txtEndDate.Text)
                , rdoEndTime.SelectedValue
                , decimal.Parse(txtCountDate.Text)
                , txtContactAddress.Text
                ,null
                ,null
                ,null
                ,null
                ,txtRemarks.Text
                ,ssMng.EmpSeqI
                ); 
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            //int chkError = Absences.Update(Int32.Parse(lblSeq.Text)
            //    , Int32.Parse(cmbAbsenceType.SelectedValue)
            //    , Int32.Parse(cmbEmpSeq.SelectedValue)
            //    , rdoStartTime.SelectedValue
            //    , rdoEndTime.SelectedValue
            //    , DateTime.Parse(txtStartDate.Text)
            //    , DateTime.Parse(txtEndDate.Text)
            //    , double.Parse(txtCountDate.Text)
            //    , txtContactAddress.Text
            //    , txtRemarks.Text
            //    , cmbRecordStatus.SelectedValue
            //    , 1);

            IAbsenceBLL Absence = new AbsenceBLL();
            IList<AbsenceInfo> AbsenceInfo = Absence.ListAbsencesBySequence(Int32.Parse(lblSeq.Text)); 
            Absence.Update(Int32.Parse(lblSeq.Text)
                , cmbAbsenceType.SelectedValue
                , cmbEmpSeq.SelectedValue
				, txtStartDate2.GetDateValue()//// DateTime.Parse(txtStartDate.Text)
                , rdoStartTime.SelectedValue
				, txtEndDate2.GetDateValue()//// DateTime.Parse(txtEndDate.Text)
                , rdoEndTime.SelectedValue
                , decimal.Parse(txtCountDate.Text)
                , txtContactAddress.Text
                , AbsenceInfo[0].Status 
                , AbsenceInfo[0].ApprovedBy
                , AbsenceInfo[0].ApprovedPosition
                , AbsenceInfo[0].ApprovedDate
                , txtRemarks.Text
                , ssMng.EmpSeqI
                );
        }


        public void AbsenceDetail()
        {
            //hrm_ps_absence[] HrmAbsence;
            ////rule_absence[] HrmAbsenceRule;
            ////string emp_seq = ssMng.EmpSeq;
            //double dblAbsence = 0.0;
            //string strEmpSeq = Session["emp_seq"].ToString();
            //string a = cmbAbsenceType.SelectedValue;
            //int intAbsenceType = Convert.ToInt32(cmbAbsenceType.SelectedValue);
            //try
            //{
                //using (var serviceClient = new AbsenceReportServicze.AbsenceReportServiczeClient())
                //{
                    //HrmAbsence = serviceClient.GetAbsenceDaysData(strEmpSeq);
                    //var Absence = (from l in HrmAbsence
                    //               where l.absence_type_seq == intAbsenceType
                    //               //where (l.start_date >= StartDate) && (l.start_date <= EndDate)
                    //               select l.day_count).Sum(l => l.Value);
                    //dblAbsence = Convert.ToDouble(Absence);
                    //lblAbsence.Text = dblAbsence.ToString();

                    // Quota //
                    //HrmAbsenceRule = serviceClient.GetDaysLimitData(emp_seq);
                    //var Quota = (from l in HrmAbsenceRule
                    //             where l.absence_type_seq == 1  // ลาป่วย
                    //             select l.day_limit).FirstOrDefault();
                    //intQuotaSick = Convert.ToInt32(Quota);
                    //lblLimitSick.Text = intQuotaSick.ToString("#");

                //}
            //}
            //catch (Exception ex)
            //{
            //    //main.MessageAlert(Page, ex.Message.ToString());
            //}

        }
    }
}
